<?php

define('PATH_BASE', './../');
define('PATH_PICTURES', PATH_BASE.'pics/');
define('PATH_BIG_PICTURES', PATH_BASE.'big-pics/');

$app->post('/manager/pictures/', 'getManagerPictures');
$app->post('/manager/picture/create/', 'createPicture');
$app->post('/manager/picture/update/', 'updatePicture');
$app->post('/manager/picture/get/:id', 'getManagerPicture');
$app->post('/manager/picture/delete/:id', 'deleteManagerPicture');


function getManagerPictures() {
	$userName = checkManagerClientCredentials();
	$sqlQuery = createManagerPictureSelect()." WHERE photographer = '$userName' ORDER BY id ASC";
	$sql = createQuery($sqlQuery);
	$result = queryToArray($sql);
	echo json_encode($result);
}

function getManagerPicture($id) {
	$userName = checkManagerClientCredentials();
	doGetManagerPicture($id, $userName);
}

function doGetManagerPicture($id, $userName) {
	$result = getPictureFromDatabase($id, $userName);
	if (!$result) {
		errorOut("ELEMENT_NOT_FOUND", "The picture with the ID $id was not found!");
	}
	echo json_encode($result);
}

function getPictureFromDatabase($id, $userName) {
	$sqlQuery = "SELECT * FROM ".TABLE_PICTURE." WHERE id = $id AND photographer = '$userName'";
	$sql = createQuery($sqlQuery);
	return mysqli_fetch_object($sql);
}

function deleteManagerPicture($id) {
	$userName = checkManagerClientCredentials();
	$sqlQuery = "DELETE FROM ".TABLE_PICTURE." WHERE id = $id AND photographer = '$userName'";
	createQuery($sqlQuery);
}


function createPicture() {
	$userName = checkManagerClientCredentials();
	if (!isset($_POST['data']))
		errorOut("INVALID_DATA", "There was no data posted!");
	$data = json_decode_or_die($_POST['data']);
	$data->photographer = $userName;
	validateFullPicture($data);
	$sectionData = getPictureData($data, 'section');
	$bigPictureData = getPictureData($data, 'bigPicture');

	$id = executeInsertPicture($data, $sectionData, $bigPictureData);
	doGetManagerPicture($id, $userName);
}


function validateFullPicture($data) {
	if (valueDoesNotExist($data, 'section')) {
		errorOut("INCOMPLETE_DATA", "The section was missing!");
	}

	foreach (getSupportedLanguagesArray() as $language) {
		$key = 'correctAnswers_'.$language;
		if (!valueDoesNotExist($data, $key)) {
			return;
		}
	}
	errorOut("INCOMPLETE_DATA", "Any description was missing!");
}

function valueDoesNotExist($data, $variable) {
	return $data == null || !array_key_exists($variable, $data) || $data->{$variable} == "";
}

function executeInsertPicture($data, $sectionData, $bigPictureData) {
	$sqlColumns = getPictureSqlColumns($data);
	$sqlValues = getPictureSqlValues($data, $sectionData, $bigPictureData);
	createQuery("INSERT INTO ".TABLE_PICTURE." ($sqlColumns) VALUES ($sqlValues)");
	return mysqli_insert_id(connectToDatabase());
}

function getPictureData($data, $variable) {
	if (!array_key_exists($variable, $data))
		return "";
	$originalData = $data->{$variable};
	$resizedData = resizePictureData($originalData, null);
	return $resizedData == null ? $originalData : getStringFromImageResource($resizedData);
}

function updatePicture() {
	$userName = checkManagerClientCredentials();
	$data = json_decode($_POST['data']);
	if (valueDoesNotExist($data, 'id'))
		errorOut("INCOMPLETE_DATA", "The ID was missing!");
	$sectionData = getPictureData($data, 'section');
	$bigPictureData = getPictureData($data, 'bigPicture');

	$id = $data->{'id'};
	executeUpdatePicture($id, $data, $sectionData, $bigPictureData);
	doGetManagerPicture($id, $userName);
}

function executeUpdatePicture($id, $data, $sectionData, $bigPictureData) {
	$setClause = getPictureSqlSetClause($data, $sectionData, $bigPictureData);
	if ($setClause != "")
		mysqli_query(connectToDatabase(), "UPDATE ".TABLE_PICTURE." SET $setClause WHERE id = $id");
}

function getPictureSqlSetClause($data, $sectionData, $bigPictureData) {
	$setClause = "";
	if ($sectionData != "") {
		$sectionData = mysqli_real_escape_string(connectToDatabase(), $sectionData);
		$setClause = $setClause."section = '$sectionData'";
	}
	if ($bigPictureData != "") {
		$bigPictureData = mysqli_real_escape_string(connectToDatabase(), $bigPictureData);
		if ($setClause != "") $setClause = $setClause.", ";
		$setClause = $setClause."bigPicture = '$bigPictureData'";
	}
	foreach (getSupportedLanguagesArray() as $language) {
		$key = 'description_'.$language;
		if (!valueDoesNotExist($data, $key)) {
			if ($setClause != "") $setClause = $setClause.", ";
			$setClause = $setClause."description_$language = '".mysqli_real_escape_string(connectToDatabase(), $data->{$key})."'";
		}
		$key = 'correctAnswers_'.$language;
		if (!valueDoesNotExist($data, $key)) {
			if ($setClause != "") $setClause = $setClause.", ";
			$setClause = $setClause."correctAnswers_$language = '".mysqli_real_escape_string(connectToDatabase(), $data->{$key})."'";
		}
	}
	return $setClause;
}


function getPictureSqlColumns($data) {
	$sqlColumns = "section, bigPicture, photographer";
	foreach (getSupportedLanguagesArray() as $language) {
		$key = 'description_'.$language;
		if (array_key_exists ($key, $data)) {
			$sqlColumns = $sqlColumns.", $key";
		}
		$key = 'correctAnswers_'.$language;
		if (array_key_exists ($key, $data)) {
			$sqlColumns = $sqlColumns.", $key";
		}
	}
	return $sqlColumns;
}

function getPictureSqlValues($data, $sectionData, $bigPictureData) {
	$sectionData = mysqli_real_escape_string(connectToDatabase(), $sectionData);
	$bigPictureData = mysqli_real_escape_string(connectToDatabase(), $bigPictureData);
	$photographer = $data->photographer;

	$sqlValues = "'$sectionData', '$bigPictureData', '$photographer'";
	foreach (getSupportedLanguagesArray() as $language) {
		$key = 'description_'.$language;
		if (array_key_exists ($key, $data)) {
			$sqlValues = $sqlValues.", '".mysqli_real_escape_string(connectToDatabase(), $data->{$key})."'";
		}
		$key = 'correctAnswers_'.$language;
		if (array_key_exists ($key, $data)) {
			$sqlValues = $sqlValues.", '".mysqli_real_escape_string(connectToDatabase(), $data->{$key})."'";
		}
	}
	return $sqlValues;
}

?>
